home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / DirectX / dxsdk_oct2004.exe / dxsdk.exe / Samples / C++ / Direct3D / HDRLighting / HDRLighting.fx < prev    next >
Encoding:
Text File  |  2004-09-27  |  32.3 KB  |  1,056 lines

  1. //-----------------------------------------------------------------------------
  2. // File: HDRLighting.fx
  3. //
  4. // Desc: Effect file for High Dynamic Range Lighting sample. This file contains
  5. //       version 2.0 pixel and vertex shaders in High-Level Shader Language.
  6. //       These shaders are used to quickly calculate the average luminance
  7. //       of the rendered scene, simulate the viewer's light adaptation level,
  8. //       map the high dynamic range of colors to a range displayable on a PC
  9. //       monitor, and perform post-process lighting effects. 
  10. //
  11. // The algorithms described in this sample are based very closely on the 
  12. // lighting effects implemented in Masaki Kawase's Rthdribl sample and the tone 
  13. // mapping process described in the whitepaper "Tone Reproduction for Digital 
  14. // Images"
  15. //
  16. // Real-Time High Dynamic Range Image-Based Lighting (Rthdribl)
  17. // Masaki Kawase
  18. // http://www.daionet.gr.jp/~masa/rthdribl/ 
  19. //
  20. // "Photographic Tone Reproduction for Digital Images"
  21. // Erik Reinhard, Mike Stark, Peter Shirley and Jim Ferwerda
  22. // http://www.cs.utah.edu/~reinhard/cdrom/ 
  23. // 
  24. // Copyright (c) Microsoft Corporation. All rights reserved.
  25. //-----------------------------------------------------------------------------
  26.  
  27.  
  28.  
  29.  
  30. //-----------------------------------------------------------------------------
  31. // Global constants
  32. //-----------------------------------------------------------------------------
  33. static const int    MAX_SAMPLES            = 16;    // Maximum texture grabs
  34. static const int    NUM_LIGHTS             = 2;     // Scene lights 
  35. static const float  BRIGHT_PASS_THRESHOLD  = 5.0f;  // Threshold for BrightPass filter
  36. static const float  BRIGHT_PASS_OFFSET     = 10.0f; // Offset for BrightPass filter
  37.  
  38. // The per-color weighting to be used for luminance calculations in RGB order.
  39. static const float3 LUMINANCE_VECTOR  = float3(0.2125f, 0.7154f, 0.0721f);
  40.  
  41. // The per-color weighting to be used for blue shift under low light.
  42. static const float3 BLUE_SHIFT_VECTOR = float3(1.05f, 0.97f, 1.27f); 
  43.  
  44.  
  45.  
  46.  
  47. //-----------------------------------------------------------------------------
  48. // Global variables
  49. //-----------------------------------------------------------------------------
  50.  
  51. // Transformation matrices
  52. float4x4 g_mObjectToView;   // Object space to view space
  53. float4x4 g_mProjection;     // View space to clip space
  54.  
  55. bool    g_bEnableTexture;   // Toggle texture modulation for current pixel
  56.  
  57. // Contains sampling offsets used by the techniques
  58. float2 g_avSampleOffsets[MAX_SAMPLES];
  59. float4 g_avSampleWeights[MAX_SAMPLES];
  60.  
  61. // Light variables
  62. float4 g_avLightPositionView[NUM_LIGHTS];   // Light positions in view space
  63. float4 g_avLightIntensity[NUM_LIGHTS];      // Floating point light intensities
  64.  
  65. float  g_fPhongExponent;        // Exponents for the phong equation
  66. float  g_fPhongCoefficient;     // Coefficient for the phong equation
  67. float  g_fDiffuseCoefficient;   // Coefficient for diffuse equation
  68. float4 g_vEmissive;             // Emissive intensity of the current light
  69.  
  70. // Tone mapping variables
  71. float  g_fMiddleGray;       // The middle gray key value
  72. float  g_fWhiteCutoff;      // Lowest luminance which is mapped to white
  73. float  g_fElapsedTime;      // Time in seconds since the last calculation
  74.  
  75. bool  g_bEnableBlueShift;   // Flag indicates if blue shift is performed
  76. bool  g_bEnableToneMap;     // Flag indicates if tone mapping is performed
  77.  
  78. float  g_fBloomScale;       // Bloom process multiplier
  79. float  g_fStarScale;        // Star process multiplier
  80.  
  81.  
  82.  
  83. //-----------------------------------------------------------------------------
  84. // Texture samplers
  85. //-----------------------------------------------------------------------------
  86. sampler s0 : register(s0);
  87. sampler s1 : register(s1);
  88. sampler s2 : register(s2);
  89. sampler s3 : register(s3);
  90. sampler s4 : register(s4);
  91. sampler s5 : register(s5);
  92. sampler s6 : register(s6);
  93. sampler s7 : register(s7);
  94.  
  95.  
  96. //-----------------------------------------------------------------------------
  97. // Vertex shaders
  98. //-----------------------------------------------------------------------------
  99.  
  100.  
  101. //-----------------------------------------------------------------------------
  102. // Name: TransformScene     
  103. // Type: Vertex shader                                      
  104. // Desc: Transforms the incoming vertex from object to clip space, and passes
  105. //       the vertex position and normal in view space on to the pixel shader
  106. //-----------------------------------------------------------------------------
  107. struct TransformSceneOutput
  108. {
  109.     float4 Position : POSITION;
  110.     float2 Texture0 : TEXCOORD0;
  111.     float3 Texture1 : TEXCOORD1;
  112.     float3 Texture2 : TEXCOORD2;
  113. };
  114.  
  115. TransformSceneOutput TransformScene
  116.     (
  117.     float3 vObjectPosition : POSITION, 
  118.     float3 vObjectNormal : NORMAL,
  119.     float2 vObjectTexture : TEXCOORD0
  120.     )
  121. {
  122.     TransformSceneOutput Output;
  123.     float4 vViewPosition;
  124.     float3 vViewNormal;
  125.   
  126.     // tranform the position/normal into view space
  127.     vViewPosition = mul(float4(vObjectPosition, 1.0f), g_mObjectToView);
  128.     vViewNormal = normalize(mul(vObjectNormal, (float3x3)g_mObjectToView));
  129.  
  130.     // project view space to screen space
  131.     Output.Position = mul(vViewPosition, g_mProjection);
  132.     
  133.     // Pass the texture coordinate without modification
  134.     Output.Texture0 = vObjectTexture;
  135.  
  136.     // Pass view position into a texture iterator
  137.     Output.Texture1 = vViewPosition.xyz;
  138.     
  139.     // Pass view surface normal into a texture iterator
  140.     Output.Texture2 = vViewNormal;
  141.     
  142.     return Output;
  143. }
  144.  
  145.  
  146.  
  147. //-----------------------------------------------------------------------------
  148. // Pixel shaders
  149. //-----------------------------------------------------------------------------
  150.  
  151.  
  152. //-----------------------------------------------------------------------------
  153. // Name: PointLight                                        
  154. // Type: Pixel shader
  155. // Desc: Per-pixel diffuse, specular, and emissive lighting
  156. //-----------------------------------------------------------------------------
  157. float4 PointLight
  158.     (
  159.     in float2 vTexture : TEXCOORD0,
  160.     in float3 vViewPosition : TEXCOORD1,
  161.     in float3 vNormal : TEXCOORD2
  162.     ) : COLOR
  163. {
  164.     float3 vPointToCamera = normalize(-vViewPosition);
  165.  
  166.     // Start with ambient term
  167.     float3 vIntensity = float3(0.02f, 0.02f, 0.02f);
  168.  
  169.     // Add emissive term to the total intensity
  170.     vIntensity += g_vEmissive; 
  171.         
  172.     for(int iLight=0; iLight < NUM_LIGHTS; iLight++)
  173.     {
  174.         // Calculate illumination variables
  175.         float3 vLightToPoint = normalize(vViewPosition - g_avLightPositionView[iLight]);
  176.         float3 vReflection   = reflect(vLightToPoint, vNormal);
  177.         float  fPhongValue   = saturate(dot(vReflection, vPointToCamera));
  178.  
  179.         // Calculate diffuse term
  180.         float  fDiffuse      = g_fDiffuseCoefficient * saturate(dot(vNormal, -vLightToPoint));
  181.  
  182.         // Calculate specular term
  183.         float  fSpecular     = g_fPhongCoefficient * pow(fPhongValue, g_fPhongExponent);
  184.         
  185.         // Scale according to distance from the light
  186.         float fDistance = distance(g_avLightPositionView[iLight], vViewPosition);
  187.         vIntensity += (fDiffuse + fSpecular) * g_avLightIntensity[iLight]/(fDistance*fDistance);
  188.     }
  189.     
  190.     // Multiply by texture color
  191.     if( g_bEnableTexture )
  192.         vIntensity *= tex2D(s0, vTexture);
  193.  
  194.     return float4(vIntensity, 1.0f);
  195. }
  196.  
  197.  
  198.  
  199.  
  200. //-----------------------------------------------------------------------------
  201. // Name: SampleLumInitial
  202. // Type: Pixel shader                                      
  203. // Desc: Sample the luminance of the source image using a kernal of sample
  204. //       points, and return a scaled image containing the log() of averages
  205. //-----------------------------------------------------------------------------
  206. float4 SampleLumInitial
  207.     (
  208.     in float2 vScreenPosition : TEXCOORD0
  209.     ) : COLOR
  210. {
  211.     float3 vSample = 0.0f;
  212.     float  fLogLumSum = 0.0f;
  213.  
  214.     for(int iSample = 0; iSample < 9; iSample++)
  215.     {
  216.         // Compute the sum of log(luminance) throughout the sample points
  217.         vSample = tex2D(s0, vScreenPosition+g_avSampleOffsets[iSample]);
  218.         fLogLumSum += log(dot(vSample, LUMINANCE_VECTOR)+0.0001f);
  219.     }
  220.     
  221.     // Divide the sum to complete the average
  222.     fLogLumSum /= 9;
  223.  
  224.     return float4(fLogLumSum, fLogLumSum, fLogLumSum, 1.0f);
  225. }
  226.  
  227.  
  228.  
  229.  
  230. //-----------------------------------------------------------------------------
  231. // Name: SampleLumIterative
  232. // Type: Pixel shader                                      
  233. // Desc: Scale down the luminance texture by blending sample points
  234. //-----------------------------------------------------------------------------
  235. float4 SampleLumIterative
  236.     (
  237.     in float2 vScreenPosition : TEXCOORD0
  238.     ) : COLOR
  239. {
  240.     float fResampleSum = 0.0f; 
  241.     
  242.     for(int iSample = 0; iSample < 16; iSample++)
  243.     {
  244.         // Compute the sum of luminance throughout the sample points
  245.         fResampleSum += tex2D(s0, vScreenPosition+g_avSampleOffsets[iSample]);
  246.     }
  247.     
  248.     // Divide the sum to complete the average
  249.     fResampleSum /= 16;
  250.  
  251.     return float4(fResampleSum, fResampleSum, fResampleSum, 1.0f);
  252. }
  253.  
  254.  
  255.  
  256.  
  257. //-----------------------------------------------------------------------------
  258. // Name: SampleLumFinal
  259. // Type: Pixel shader                                      
  260. // Desc: Extract the average luminance of the image by completing the averaging
  261. //       and taking the exp() of the result
  262. //-----------------------------------------------------------------------------
  263. float4 SampleLumFinal
  264.     (
  265.     in float2 vScreenPosition : TEXCOORD0
  266.     ) : COLOR
  267. {
  268.     float fResampleSum = 0.0f;
  269.     
  270.     for(int iSample = 0; iSample < 16; iSample++)
  271.     {
  272.         // Compute the sum of luminance throughout the sample points
  273.         fResampleSum += tex2D(s0, vScreenPosition+g_avSampleOffsets[iSample]);
  274.     }
  275.     
  276.     // Divide the sum to complete the average, and perform an exp() to complete
  277.     // the average luminance calculation
  278.     fResampleSum = exp(fResampleSum/16);
  279.     
  280.     return float4(fResampleSum, fResampleSum, fResampleSum, 1.0f);
  281. }
  282.  
  283.  
  284.  
  285.  
  286. //-----------------------------------------------------------------------------
  287. // Name: CalculateAdaptedLum
  288. // Type: Pixel shader                                      
  289. // Desc: Calculate the luminance that the camera is current adapted to, using
  290. //       the most recented adaptation level, the current scene luminance, and
  291. //       the time elapsed since last calculated
  292. //-----------------------------------------------------------------------------
  293. float4 CalculateAdaptedLum
  294.     (
  295.     in float2 vScreenPosition : TEXCOORD0
  296.     ) : COLOR
  297. {
  298.     float fAdaptedLum = tex2D(s0, float2(0.5f, 0.5f));
  299.     float fCurrentLum = tex2D(s1, float2(0.5f, 0.5f));
  300.     
  301.     // The user's adapted luminance level is simulated by closing the gap between
  302.     // adapted luminance and current luminance by 2% every frame, based on a
  303.     // 30 fps rate. This is not an accurate model of human adaptation, which can
  304.     // take longer than half an hour.
  305.     float fNewAdaptation = fAdaptedLum + (fCurrentLum - fAdaptedLum) * ( 1 - pow( 0.98f, 30 * g_fElapsedTime ) );
  306.     return float4(fNewAdaptation, fNewAdaptation, fNewAdaptation, 1.0f);
  307. }
  308.  
  309.  
  310.  
  311.  
  312. //-----------------------------------------------------------------------------
  313. // Name: FinalScenePass
  314. // Type: Pixel shader                                      
  315. // Desc: Perform blue shift, tone map the scene, and add post-processed light
  316. //       effects
  317. //-----------------------------------------------------------------------------
  318. float4 FinalScenePass
  319.     (
  320.     in float2 vScreenPosition : TEXCOORD0
  321.     ) : COLOR
  322. {
  323.     float4 vSample = tex2D(s0, vScreenPosition);
  324.     float4 vBloom = tex2D(s1, vScreenPosition);
  325.     float4 vStar = tex2D(s2, vScreenPosition);
  326.     float fAdaptedLum = tex2D(s3, float2(0.5f, 0.5f));
  327.  
  328.     // For very low light conditions, the rods will dominate the perception
  329.     // of light, and therefore color will be desaturated and shifted
  330.     // towards blue.
  331.     if( g_bEnableBlueShift )
  332.     {
  333.         // Define a linear blending from -1.5 to 2.6 (log scale) which
  334.         // determines the lerp amount for blue shift
  335.         float fBlueShiftCoefficient = 1.0f - (fAdaptedLum + 1.5)/4.1;
  336.         fBlueShiftCoefficient = saturate(fBlueShiftCoefficient);
  337.  
  338.         // Lerp between current color and blue, desaturated copy
  339.         float3 vRodColor = dot( (float3)vSample, LUMINANCE_VECTOR ) * BLUE_SHIFT_VECTOR;
  340.         vSample.rgb = lerp( (float3)vSample, vRodColor, fBlueShiftCoefficient );
  341.     }
  342.     
  343.     
  344.     // Map the high range of color values into a range appropriate for
  345.     // display, taking into account the user's adaptation level, and selected
  346.     // values for for middle gray and white cutoff.
  347.     if( g_bEnableToneMap )
  348.     {
  349.         vSample.rgb *= g_fMiddleGray/(fAdaptedLum + 0.001f);
  350.         vSample.rgb /= (1.0f+vSample);
  351.     }  
  352.     
  353.     // Add the star and bloom post processing effects
  354.     vSample += g_fStarScale * vStar;
  355.     vSample += g_fBloomScale * vBloom;
  356.     
  357.     return vSample;
  358. }
  359.  
  360.  
  361.  
  362.  
  363. //-----------------------------------------------------------------------------
  364. // Name: DownScale4x4
  365. // Type: Pixel shader                                      
  366. // Desc: Scale the source texture down to 1/16 scale
  367. //-----------------------------------------------------------------------------
  368. float4 DownScale4x4
  369.     (
  370.     in float2 vScreenPosition : TEXCOORD0
  371.     ) : COLOR
  372. {
  373.     
  374.     float4 sample = 0.0f;
  375.  
  376.     for( int i=0; i < 16; i++ )
  377.     {
  378.         sample += tex2D( s0, vScreenPosition + g_avSampleOffsets[i] );
  379.     }
  380.     
  381.     return sample / 16;
  382. }
  383.  
  384.  
  385.  
  386.  
  387. //-----------------------------------------------------------------------------
  388. // Name: DownScale2x2
  389. // Type: Pixel shader                                      
  390. // Desc: Scale the source texture down to 1/4 scale
  391. //-----------------------------------------------------------------------------
  392. float4 DownScale2x2
  393.     (
  394.     in float2 vScreenPosition : TEXCOORD0
  395.     ) : COLOR
  396. {
  397.     
  398.     float4 sample = 0.0f;
  399.  
  400.     for( int i=0; i < 4; i++ )
  401.     {
  402.         sample += tex2D( s0, vScreenPosition + g_avSampleOffsets[i] );
  403.     }
  404.     
  405.     return sample / 4;
  406. }
  407.  
  408.  
  409.  
  410.  
  411. //-----------------------------------------------------------------------------
  412. // Name: GaussBlur5x5
  413. // Type: Pixel shader                                      
  414. // Desc: Simulate a 5x5 kernel gaussian blur by sampling the 12 points closest
  415. //       to the center point.
  416. //-----------------------------------------------------------------------------
  417. float4 GaussBlur5x5
  418.     (
  419.     in float2 vScreenPosition : TEXCOORD0
  420.     ) : COLOR
  421. {
  422.     
  423.     float4 sample = 0.0f;
  424.  
  425.     for( int i=0; i < 12; i++ )
  426.     {
  427.         sample += g_avSampleWeights[i] * tex2D( s0, vScreenPosition + g_avSampleOffsets[i] );
  428.     }
  429.  
  430.     return sample;
  431. }
  432.  
  433.  
  434.  
  435.  
  436. //-----------------------------------------------------------------------------
  437. // Name: BrightPassFilter
  438. // Type: Pixel shader                                      
  439. // Desc: Perform a high-pass filter on the source texture
  440. //-----------------------------------------------------------------------------
  441. float4 BrightPassFilter
  442.     (
  443.     in float2 vScreenPosition : TEXCOORD0
  444.     ) : COLOR
  445. {
  446.     float4 vSample = tex2D( s0, vScreenPosition );
  447.     float  fAdaptedLum = tex2D( s1, float2(0.5f, 0.5f) );
  448.     
  449.     // Determine what the pixel's value will be after tone-mapping occurs
  450.     vSample.rgb *= g_fMiddleGray/(fAdaptedLum + 0.001f);
  451.     
  452.     // Subtract out dark pixels
  453.     vSample.rgb -= BRIGHT_PASS_THRESHOLD;
  454.     
  455.     // Clamp to 0
  456.     vSample = max(vSample, 0.0f);
  457.     
  458.     // Map the resulting value into the 0 to 1 range. Higher values for
  459.     // BRIGHT_PASS_OFFSET will isolate lights from illuminated scene 
  460.     // objects.
  461.     vSample.rgb /= (BRIGHT_PASS_OFFSET+vSample);
  462.     
  463.     return vSample;
  464. }
  465.  
  466.  
  467.  
  468.  
  469. //-----------------------------------------------------------------------------
  470. // Name: Bloom
  471. // Type: Pixel shader                                      
  472. // Desc: Blur the source image along one axis using a gaussian
  473. //       distribution. Since gaussian blurs are separable, this shader is called 
  474. //       twice; first along the horizontal axis, then along the vertical axis.
  475. //-----------------------------------------------------------------------------
  476. float4 Bloom
  477.     (
  478.     in float2 vScreenPosition : TEXCOORD0
  479.     ) : COLOR
  480. {
  481.     
  482.     float4 vSample = 0.0f;
  483.     float4 vColor = 0.0f;
  484.         
  485.     float2 vSamplePosition;
  486.     
  487.     // Perform a one-directional gaussian blur
  488.     for(int iSample = 0; iSample < 15; iSample++)
  489.     {
  490.         vSamplePosition = vScreenPosition + g_avSampleOffsets[iSample];
  491.         vColor = tex2D(s0, vSamplePosition);
  492.         vSample += g_avSampleWeights[iSample]*vColor;
  493.     }
  494.     
  495.     return vSample;
  496. }
  497.  
  498.  
  499.  
  500.  
  501. //-----------------------------------------------------------------------------
  502. // Name: Star
  503. // Type: Pixel shader                                      
  504. // Desc: Each star is composed of up to 8 lines, and each line is created by
  505. //       up to three passes of this shader, which samples from 8 points along
  506. //       the current line.
  507. //-----------------------------------------------------------------------------
  508. float4 Star
  509.     (
  510.     in float2 vScreenPosition : TEXCOORD0
  511.     ) : COLOR
  512. {
  513.     float4 vSample = 0.0f;
  514.     float4 vColor = 0.0f;
  515.         
  516.     float2 vSamplePosition;
  517.     
  518.     // Sample from eight points along the star line
  519.     for(int iSample = 0; iSample < 8; iSample++)
  520.     {
  521.         vSamplePosition = vScreenPosition + g_avSampleOffsets[iSample];
  522.         vSample = tex2D(s0, vSamplePosition);
  523.         vColor += g_avSampleWeights[iSample] * vSample;
  524.     }
  525.         
  526.     return vColor;
  527. }
  528.  
  529.  
  530.  
  531.  
  532. //-----------------------------------------------------------------------------
  533. // Name: MergeTextures_N
  534. // Type: Pixel shader                                      
  535. // Desc: Return the average of N input textures
  536. //-----------------------------------------------------------------------------
  537. float4 MergeTextures_1
  538.     (
  539.     in float2 vScreenPosition : TEXCOORD0
  540.     ) : COLOR
  541. {
  542.     float4 vColor = 0.0f;
  543.     
  544.     vColor += g_avSampleWeights[0] * tex2D(s0, vScreenPosition);
  545.         
  546.     return vColor;
  547. }
  548.  
  549.  
  550.  
  551.  
  552. //-----------------------------------------------------------------------------
  553. // Name: MergeTextures_N
  554. // Type: Pixel shader                                      
  555. // Desc: Return the average of N input textures
  556. //-----------------------------------------------------------------------------
  557. float4 MergeTextures_2
  558.     (
  559.     in float2 vScreenPosition : TEXCOORD0
  560.     ) : COLOR
  561. {
  562.     float4 vColor = 0.0f;
  563.     
  564.     vColor += g_avSampleWeights[0] * tex2D(s0, vScreenPosition);
  565.     vColor += g_avSampleWeights[1] * tex2D(s1, vScreenPosition);
  566.         
  567.     return vColor;
  568. }
  569.  
  570.  
  571.  
  572.  
  573. //-----------------------------------------------------------------------------
  574. // Name: MergeTextures_N
  575. // Type: Pixel shader                                      
  576. // Desc: Return the average of N input textures
  577. //-----------------------------------------------------------------------------
  578. float4 MergeTextures_3
  579.     (
  580.     in float2 vScreenPosition : TEXCOORD0
  581.     ) : COLOR
  582. {
  583.     float4 vColor = 0.0f;
  584.     
  585.     vColor += g_avSampleWeights[0] * tex2D(s0, vScreenPosition);
  586.     vColor += g_avSampleWeights[1] * tex2D(s1, vScreenPosition);
  587.     vColor += g_avSampleWeights[2] * tex2D(s2, vScreenPosition);
  588.         
  589.     return vColor;
  590. }
  591.  
  592.  
  593.  
  594.  
  595. //-----------------------------------------------------------------------------
  596. // Name: MergeTextures_N
  597. // Type: Pixel shader                                      
  598. // Desc: Return the average of N input textures
  599. //-----------------------------------------------------------------------------
  600. float4 MergeTextures_4
  601.     (
  602.     in float2 vScreenPosition : TEXCOORD0
  603.     ) : COLOR
  604. {
  605.     float4 vColor = 0.0f;
  606.     
  607.     vColor += g_avSampleWeights[0] * tex2D(s0, vScreenPosition);
  608.     vColor += g_avSampleWeights[1] * tex2D(s1, vScreenPosition);
  609.     vColor += g_avSampleWeights[2] * tex2D(s2, vScreenPosition);
  610.     vColor += g_avSampleWeights[3] * tex2D(s3, vScreenPosition);
  611.         
  612.     return vColor;
  613. }
  614.  
  615.  
  616.  
  617.  
  618. //-----------------------------------------------------------------------------
  619. // Name: MergeTextures_N
  620. // Type: Pixel shader                                      
  621. // Desc: Return the average of N input textures
  622. //-----------------------------------------------------------------------------
  623. float4 MergeTextures_5
  624.     (
  625.     in float2 vScreenPosition : TEXCOORD0
  626.     ) : COLOR
  627. {
  628.     float4 vColor = 0.0f;
  629.     
  630.     vColor += g_avSampleWeights[0] * tex2D(s0, vScreenPosition);
  631.     vColor += g_avSampleWeights[1] * tex2D(s1, vScreenPosition);
  632.     vColor += g_avSampleWeights[2] * tex2D(s2, vScreenPosition);
  633.     vColor += g_avSampleWeights[3] * tex2D(s3, vScreenPosition);
  634.     vColor += g_avSampleWeights[4] * tex2D(s4, vScreenPosition);
  635.         
  636.     return vColor;
  637. }
  638.  
  639.  
  640.  
  641.  
  642. //-----------------------------------------------------------------------------
  643. // Name: MergeTextures_N
  644. // Type: Pixel shader                                      
  645. // Desc: Return the average of N input textures
  646. //-----------------------------------------------------------------------------
  647. float4 MergeTextures_6
  648.     (
  649.     in float2 vScreenPosition : TEXCOORD0
  650.     ) : COLOR
  651. {
  652.     float4 vColor = 0.0f;
  653.     
  654.     vColor += g_avSampleWeights[0] * tex2D(s0, vScreenPosition);
  655.     vColor += g_avSampleWeights[1] * tex2D(s1, vScreenPosition);
  656.     vColor += g_avSampleWeights[2] * tex2D(s2, vScreenPosition);
  657.     vColor += g_avSampleWeights[3] * tex2D(s3, vScreenPosition);
  658.     vColor += g_avSampleWeights[4] * tex2D(s4, vScreenPosition);
  659.     vColor += g_avSampleWeights[5] * tex2D(s5, vScreenPosition);
  660.         
  661.     return vColor;
  662. }
  663.  
  664.  
  665.  
  666.  
  667. //-----------------------------------------------------------------------------
  668. // Name: MergeTextures_N
  669. // Type: Pixel shader                                      
  670. // Desc: Return the average of N input textures
  671. //-----------------------------------------------------------------------------
  672. float4 MergeTextures_7
  673.     (
  674.     in float2 vScreenPosition : TEXCOORD0
  675.     ) : COLOR
  676. {
  677.     float4 vColor = 0.0f;
  678.     
  679.     vColor += g_avSampleWeights[0] * tex2D(s0, vScreenPosition);
  680.     vColor += g_avSampleWeights[1] * tex2D(s1, vScreenPosition);
  681.     vColor += g_avSampleWeights[2] * tex2D(s2, vScreenPosition);
  682.     vColor += g_avSampleWeights[3] * tex2D(s3, vScreenPosition);
  683.     vColor += g_avSampleWeights[4] * tex2D(s4, vScreenPosition);
  684.     vColor += g_avSampleWeights[5] * tex2D(s5, vScreenPosition);
  685.     vColor += g_avSampleWeights[6] * tex2D(s6, vScreenPosition);
  686.         
  687.     return vColor;
  688. }
  689.  
  690.  
  691.  
  692.  
  693. //-----------------------------------------------------------------------------
  694. // Name: MergeTextures_N
  695. // Type: Pixel shader                                      
  696. // Desc: Return the average of N input textures
  697. //-----------------------------------------------------------------------------
  698. float4 MergeTextures_8
  699.     (
  700.     in float2 vScreenPosition : TEXCOORD0
  701.     ) : COLOR
  702. {
  703.     float4 vColor = 0.0f;
  704.     
  705.     vColor += g_avSampleWeights[0] * tex2D(s0, vScreenPosition);
  706.     vColor += g_avSampleWeights[1] * tex2D(s1, vScreenPosition);
  707.     vColor += g_avSampleWeights[2] * tex2D(s2, vScreenPosition);
  708.     vColor += g_avSampleWeights[3] * tex2D(s3, vScreenPosition);
  709.     vColor += g_avSampleWeights[4] * tex2D(s4, vScreenPosition);
  710.     vColor += g_avSampleWeights[5] * tex2D(s5, vScreenPosition);
  711.     vColor += g_avSampleWeights[6] * tex2D(s6, vScreenPosition);
  712.     vColor += g_avSampleWeights[7] * tex2D(s7, vScreenPosition);
  713.         
  714.     return vColor;
  715. }
  716.  
  717.  
  718.  
  719.  
  720. //-----------------------------------------------------------------------------
  721. // Techniques
  722. //-----------------------------------------------------------------------------
  723.  
  724.  
  725. //-----------------------------------------------------------------------------
  726. // Name: RenderScene
  727. // Type: Technique                                     
  728. // Desc: Performs specular lighting
  729. //-----------------------------------------------------------------------------
  730. technique RenderScene
  731. {
  732.     pass P0
  733.     {        
  734.         VertexShader = compile vs_2_0 TransformScene();
  735.         PixelShader  = compile ps_2_0 PointLight();
  736.     }
  737. }
  738.  
  739.  
  740.  
  741.  
  742. //-----------------------------------------------------------------------------
  743. // Name: Bloom
  744. // Type: Technique                                     
  745. // Desc: Performs a single horizontal or vertical pass of the blooming filter
  746. //-----------------------------------------------------------------------------
  747. technique Bloom
  748. {
  749.     pass P0
  750.     {        
  751.         PixelShader  = compile ps_2_0 Bloom();
  752.     }
  753.  
  754. }
  755.  
  756.  
  757.  
  758. //-----------------------------------------------------------------------------
  759. // Name: Star
  760. // Type: Technique                                     
  761. // Desc: Perform one of up to three passes composing the current star line
  762. //-----------------------------------------------------------------------------
  763. technique Star
  764. {
  765.     pass P0
  766.     {        
  767.         PixelShader  = compile ps_2_0 Star();
  768.     }
  769.  
  770. }
  771.  
  772.  
  773.  
  774.  
  775.  
  776. //-----------------------------------------------------------------------------
  777. // Name: SampleAvgLum
  778. // Type: Technique                                     
  779. // Desc: Takes the HDR Scene texture as input and starts the process of 
  780. //       determining the average luminance by converting to grayscale, taking
  781. //       the log(), and scaling the image to a single pixel by averaging sample 
  782. //       points.
  783. //-----------------------------------------------------------------------------
  784. technique SampleAvgLum
  785. {
  786.     pass P0
  787.     {
  788.         PixelShader  = compile ps_2_0 SampleLumInitial();
  789.     }
  790. }
  791.  
  792.  
  793.  
  794.  
  795. //-----------------------------------------------------------------------------
  796. // Name: ResampleAvgLum
  797. // Type: Technique                                     
  798. // Desc: Continue to scale down the luminance texture
  799. //-----------------------------------------------------------------------------
  800. technique ResampleAvgLum
  801. {
  802.     pass P0
  803.     {
  804.         PixelShader  = compile ps_2_0 SampleLumIterative();
  805.     }
  806. }
  807.  
  808.  
  809.  
  810.  
  811. //-----------------------------------------------------------------------------
  812. // Name: ResampleAvgLumExp
  813. // Type: Technique                                     
  814. // Desc: Sample the texture to a single pixel and perform an exp() to complete
  815. //       the evalutation
  816. //-----------------------------------------------------------------------------
  817. technique ResampleAvgLumExp
  818. {
  819.     pass P0
  820.     {
  821.         PixelShader  = compile ps_2_0 SampleLumFinal();
  822.     }
  823. }
  824.  
  825.  
  826.  
  827.  
  828. //-----------------------------------------------------------------------------
  829. // Name: CalculateAdaptedLum
  830. // Type: Technique                                     
  831. // Desc: Determines the level of the user's simulated light adaptation level
  832. //       using the last adapted level, the current scene luminance, and the
  833. //       time since last calculation
  834. //-----------------------------------------------------------------------------
  835. technique CalculateAdaptedLum
  836. {
  837.     pass P0
  838.     {
  839.         PixelShader  = compile ps_2_0 CalculateAdaptedLum();
  840.     }
  841. }
  842.  
  843.  
  844.  
  845.  
  846. //-----------------------------------------------------------------------------
  847. // Name: DownScale4x4
  848. // Type: Technique                                     
  849. // Desc: Scale the source texture down to 1/16 scale
  850. //-----------------------------------------------------------------------------
  851. technique DownScale4x4
  852. {
  853.     pass P0
  854.     {
  855.         PixelShader  = compile ps_2_0 DownScale4x4();
  856.     }
  857. }
  858.  
  859.  
  860.  
  861.  
  862. //-----------------------------------------------------------------------------
  863. // Name: DownScale2x2
  864. // Type: Technique                                     
  865. // Desc: Scale the source texture down to 1/4 scale
  866. //-----------------------------------------------------------------------------
  867. technique DownScale2x2
  868. {
  869.     pass P0
  870.     {
  871.         PixelShader  = compile ps_2_0 DownScale2x2();
  872.     }
  873. }
  874.  
  875.  
  876.  
  877.  
  878. //-----------------------------------------------------------------------------
  879. // Name: GaussBlur5x5
  880. // Type: Technique                                     
  881. // Desc: Simulate a 5x5 kernel gaussian blur by sampling the 12 points closest
  882. //       to the center point.
  883. //-----------------------------------------------------------------------------
  884. technique GaussBlur5x5
  885. {
  886.     pass P0
  887.     {
  888.         PixelShader  = compile ps_2_0 GaussBlur5x5();
  889.     }
  890. }
  891.  
  892.  
  893.  
  894.  
  895. //-----------------------------------------------------------------------------
  896. // Name: BrightPassFilter
  897. // Type: Technique                                     
  898. // Desc: Perform a high-pass filter on the source texture
  899. //-----------------------------------------------------------------------------
  900. technique BrightPassFilter
  901. {
  902.     pass P0
  903.     {
  904.         PixelShader  = compile ps_2_0 BrightPassFilter();
  905.     }
  906. }
  907.  
  908.  
  909.  
  910.  
  911.  
  912. //-----------------------------------------------------------------------------
  913. // Name: FinalScenePass
  914. // Type: Technique                                     
  915. // Desc: Minimally transform and texture the incoming geometry
  916. //-----------------------------------------------------------------------------
  917. technique FinalScenePass
  918. {
  919.     pass P0
  920.     {
  921.         PixelShader  = compile ps_2_0 FinalScenePass();
  922.     }
  923. }
  924.  
  925.  
  926.  
  927.  
  928. //-----------------------------------------------------------------------------
  929. // Name: MergeTextures_N
  930. // Type: Technique                                     
  931. // Desc: Return the average of N input textures
  932. //-----------------------------------------------------------------------------
  933. technique MergeTextures_1
  934. {
  935.     pass P0
  936.     {        
  937.         PixelShader  = compile ps_2_0 MergeTextures_1();
  938.     }
  939.  
  940. }
  941.  
  942.  
  943.  
  944.  
  945. //-----------------------------------------------------------------------------
  946. // Name: MergeTextures_N
  947. // Type: Technique                                     
  948. // Desc: Return the average of N input textures
  949. //-----------------------------------------------------------------------------
  950. technique MergeTextures_2
  951. {
  952.     pass P0
  953.     {        
  954.         PixelShader  = compile ps_2_0 MergeTextures_2();
  955.     }
  956.  
  957. }
  958.  
  959.  
  960.  
  961.  
  962. //-----------------------------------------------------------------------------
  963. // Name: MergeTextures_N
  964. // Type: Technique                                     
  965. // Desc: Return the average of N input textures
  966. //-----------------------------------------------------------------------------
  967. technique MergeTextures_3
  968. {
  969.     pass P0
  970.     {        
  971.         PixelShader  = compile ps_2_0 MergeTextures_3();
  972.     }
  973.  
  974. }
  975.  
  976.  
  977.  
  978.  
  979. //-----------------------------------------------------------------------------
  980. // Name: MergeTextures_N
  981. // Type: Technique                                     
  982. // Desc: Return the average of N input textures
  983. //-----------------------------------------------------------------------------
  984. technique MergeTextures_4
  985. {
  986.     pass P0
  987.     {        
  988.         PixelShader  = compile ps_2_0 MergeTextures_4();
  989.     }
  990.  
  991. }
  992.  
  993.  
  994.  
  995.  
  996. //-----------------------------------------------------------------------------
  997. // Name: MergeTextures_N
  998. // Type: Technique                                     
  999. // Desc: Return the average of N input textures
  1000. //-----------------------------------------------------------------------------
  1001. technique MergeTextures_5
  1002. {
  1003.     pass P0
  1004.     {        
  1005.         PixelShader  = compile ps_2_0 MergeTextures_5();
  1006.     }
  1007.  
  1008. }
  1009.  
  1010.  
  1011.  
  1012.  
  1013. //-----------------------------------------------------------------------------
  1014. // Name: MergeTextures_N
  1015. // Type: Technique                                     
  1016. // Desc: Return the average of N input textures
  1017. //-----------------------------------------------------------------------------
  1018. technique MergeTextures_6
  1019. {
  1020.     pass P0
  1021.     {        
  1022.         PixelShader  = compile ps_2_0 MergeTextures_6();
  1023.     }
  1024.  
  1025. }
  1026.  
  1027.  
  1028.  
  1029.  
  1030. //-----------------------------------------------------------------------------
  1031. // Name: MergeTextures_N
  1032. // Type: Technique                                     
  1033. // Desc: Return the average of N input textures
  1034. //-----------------------------------------------------------------------------
  1035. technique MergeTextures_7
  1036. {
  1037.     pass P0
  1038.     {        
  1039.         PixelShader  = compile ps_2_0 MergeTextures_7();
  1040.     }
  1041.  
  1042. }
  1043.  
  1044.  
  1045.  
  1046.  
  1047. //-----------------------------------------------------------------------------
  1048. // Name: MergeTextures_N
  1049. // Type: Technique                                     
  1050. // Desc: Return the average of N input textures
  1051. //-----------------------------------------------------------------------------
  1052. technique MergeTextures_8
  1053. {
  1054.     pass P0
  1055.     {        
  1056.         PixelShader  = compile ps_2_0 MergeTextures_8();
  1057.     }
  1058.  
  1059. }